package com.liaoban.app.ui.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.event.UpdateUserEvent;
import com.liaoban.app.api.type.request.SettingInfoRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ImageHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.ui.base.BaseUploadPicActivity;
import com.liaoban.app.util.CommonUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.picker.NumberPicker;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * Created by ybc on 2018/7/10 0010.
 * 编辑资料
 */
public class EditDataActivity extends BaseUploadPicActivity {
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.iv_src)
    ImageView ivSrc;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_grade)
    TextView tvGrade;

    @Override
    protected String getNewTitle() {
        return "编辑资料";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_edit_data;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ivRight.setImageResource(R.mipmap.ic_confirm);
        ivRight.setVisibility(View.VISIBLE);
        setView();
    }

    private void setView() {
        String src = ConfigHelper.getString(this, ConfigKey.USER_SRC);
        String name = ConfigHelper.getString(this, ConfigKey.USER_NAME);
        String sex = ConfigHelper.getString(this, ConfigKey.USER_SEX);
        String age = ConfigHelper.getString(this, ConfigKey.USER_AGE);
        String grade = ConfigHelper.getString(this, ConfigKey.USER_GRADE);
        ImageHelper.load(ivSrc, CommonUtils.getFullPic(src));
        etName.setText(name);
        tvSex.setText(CommonUtils.getSex(sex));
        tvAge.setText(age);
        tvGrade.setText("Lv." + grade);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String desc = ConfigHelper.getString(this, ConfigKey.USER_DESC);
        tvDescription.setText(desc);
    }

    @OnClick(R.id.layout_introduce)
    public void introduce() {
        startActivity(new Intent(this, IntroduceActivity.class));
    }

    @OnClick(R.id.iv_right)
    public void right() {
        String name = etName.getText().toString().trim();
        String sex = "女".equals(tvSex.getText().toString().trim()) ? "1" : "2";
        String age = tvAge.getText().toString().trim();
        String desc = tvDescription.getText().toString().trim();
        SettingInfoRequest settingInfoRequest = new SettingInfoRequest();
        settingInfoRequest.setWuserName(name);
        settingInfoRequest.setAge(age);
        settingInfoRequest.setDescription(desc);
        settingInfoRequest.setWuserSex(sex);
        showRunningDialog(R.string.committing);
        startTask(CommonBiz.getInstance().settingInfoSubmit(settingInfoRequest), baseResponse -> {
            ToastHelper.show(EditDataActivity.this, baseResponse.msg);
            ConfigHelper.save(EditDataActivity.this, ConfigKey.USER_NAME, name);
            ConfigHelper.save(EditDataActivity.this, ConfigKey.USER_SEX, sex);
            ConfigHelper.save(EditDataActivity.this, ConfigKey.USER_AGE, age);
            finish();
        });
    }

    @OnClick(R.id.layout_name)
    public void name() {
        etName.requestFocus();
    }

    @OnClick(R.id.layout_sex)
    public void sex() {
        SinglePicker<String> picker = new SinglePicker<>(this,
                new String[]{
                        "男", "女"
                });
        picker.setCanLoop(true);//不禁用循环
        picker.setTopBackgroundColor(getResources().getColor(R.color.white));
        picker.setTopHeight(50);
        picker.setTopLineColor(getResources().getColor(R.color.line));
        picker.setTopLineHeight(1);
        picker.setTitleText("请选择");
        picker.setTitleTextColor(getResources().getColor(R.color.text_black));
        picker.setTitleTextSize(15);
        picker.setCancelTextColor(getResources().getColor(R.color.text_black));
        picker.setCancelTextSize(13);
        picker.setSubmitTextColor(getResources().getColor(R.color.text_black));
        picker.setSubmitTextSize(13);
        picker.setSelectedTextColor(getResources().getColor(R.color.btn_yellow));
        picker.setUnSelectedTextColor(getResources().getColor(R.color.text_gray));
        LineConfig config = new LineConfig();
        config.setColor(getResources().getColor(R.color.btn_yellow));//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setItemWidth(200);
        picker.setBackgroundColor(getResources().getColor(R.color.white));
        picker.setOnItemPickListener((index, item) -> {
            tvSex.setText(item);
        });
        picker.show();
    }

    @OnClick(R.id.layout_age)
    public void age() {
        NumberPicker picker = new NumberPicker(this);
        picker.setRange(16, 100, 1);//数字范围
        picker.setSelectedItem(18);
        picker.setLabel("岁");
        picker.setSelectedTextColor(getResources().getColor(R.color.btn_yellow));
        picker.setUnSelectedTextColor(getResources().getColor(R.color.text_gray));
        LineConfig config = new LineConfig();
        config.setColor(getResources().getColor(R.color.btn_yellow));//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setItemWidth(200);
        picker.setBackgroundColor(getResources().getColor(R.color.white));
        picker.setOnNumberPickListener(new NumberPicker.OnNumberPickListener() {
            @Override
            public void onNumberPicked(int index, Number item) {
                tvAge.setText(item + "");
            }
        });
        picker.show();
    }

    @OnClick(R.id.layout_src)
    public void src() {
        showSheetDialog(true);
    }

    @Override
    protected void uploadPicSuccessful(boolean isTailor, File outFile, Uri filePath) {
        showRunningDialog(R.string.committing);
        startTask(CommonBiz.getInstance().headImgUpload(outFile), baseResponse -> {
            ToastHelper.show(this, baseResponse.msg);
            if(1 == baseResponse.code) {
                ImageHelper.load(ivSrc, String.valueOf(outFile));
                ConfigHelper.save(EditDataActivity.this, ConfigKey.USER_SRC, baseResponse.data);
                EventBus.getDefault().post(new UpdateUserEvent(ConfigKey.USER_SRC));
            }
        });

    }

    @OnClick(R.id.layout_grade)
    public void grade() {
        startActivity(GradeDetailActivity.newIntent(this));
    }
}
